﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


namespace Login.Database.JsonConvert
{
    public class DynamicEntityContractResolver : DefaultContractResolver
    {
        public DynamicEntityContractResolver() { }


        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
            // only serializer properties that start with the specified character

            var entityProperties = type.GetProperties();
            var virtualPropeties = entityProperties.Where(p => p.GetMethod.IsVirtual || p.GetCustomAttributes(typeof(NotMappedAttribute)).Any());
            var vpname = virtualPropeties.Select(p => p.Name);

            properties =
              properties.Where(p => !vpname.Contains(p.PropertyName)).ToList();

            return properties;
        }
    }

}
